SlideShare a Scribd company logo
Class No.17  Data Structures http://ecomputernotes.com
Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is the address operator, i.e., it returns the address of the variable in memory. int x; int* ptr = &x; http://ecomputernotes.com
Reference Variables The symbol “&’ can also appear after a type in a function signature: For example, insert and remove from the BinarySearchTree class. void insert( const EType& x ); void remove( const EType& x ); http://ecomputernotes.com
Reference Variables Or, in case we designed the BinarySearchTree class to hold integers only, i.e., no templates  void insert( const int& x ); void remove( const int& x ); http://ecomputernotes.com
Reference Variables The “&” indicates a parameter that is a  reference variable . Consider the following three different functions: http://ecomputernotes.com
Reference Variables // example 1 int intMinus1( int oldVal) { oldVal = oldVal – 1; return oldVal; } http://ecomputernotes.com
Reference Variables // example 2 int intMinus2( int* oldVal) { *oldVal = *oldVal – 2; return *oldVal; } http://ecomputernotes.com
Reference Variables // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
Reference Variables The caller function: calling  intMinus1 void caller() { int myInt = 31; int retVal; retVal = intMinus1( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (ourtest.exe) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
Reference Variables Call stack layout Parameters(caller) Local variables(caller) Return address(caller) Parameters(intMinus1) Local variables(intMinus1) Return address(intMinus1) sp Stack grows downwards http://ecomputernotes.com
Reference Variables Call stack layout when intMinus1 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards   sp
Reference Variables How are myInt and oldVal related? Passing myInt to the function intMinus1 results in a  copy  of myInt to be placed in parameter oldVal in the call stack. Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
Reference Variables The original myInt remains  unchanged . For this reason, this technique of passing parameters is called  pass by value . Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
Reference Variables Call stack layout after subtraction in intMinus1: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards   sp 30
Reference Variables Call stack layout after return from intMinus1: 1068 31 1072 30 caller’s other stuff   myInt retVal calling function “caller” stack grows downwards   sp
Reference Variables We could have called  intMinus1  as void caller() { int retVal; retVal = intMinus1( 31 );// literal cout << myInt << retVal; } Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value. http://ecomputernotes.com
Reference Variables If the programmer wanted to actually change a variable’s value from within a function, one way would be to send a pointer: void caller() { int retVal; int myInt = 31; retVal = intMinus2( &myInt ); cout << myInt << retVal; } Call this call-by-pointer. http://ecomputernotes.com
Reference Variables Call stack layout when intMinus2 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards   sp
Reference Variables Call stack layout after  *oldVal = *oldVal – 2; 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards   sp 29
Reference Variables Call stack layout after return from  intMinus2. 1068 31 1072 29 caller’s other stuff   myInt retVal calling function “caller” stack grows downwards   sp 29
Reference Variables Suppose we want a function to change an object. But we don’t want to send the function a copy. The object could be large and copying it costs time. We don’t want to use pointers because of the messy syntax. http://ecomputernotes.com
Reference Variables The answer:  call-by-reference  (or pass-by-reference): void caller() { int retVal; int myInt = 31; retVal = intMinus3( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
Reference Variables The & after int means that oldVal is an integer reference variable. // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
Reference Variables So what  is  a reference variable? The idea is: the integer object myInt is used exactly as it exists in the caller. The function simply reaches it through a  different name , oldVal. The function intMinus3 cannot use the name myInt because it is in the caller’s scope. But both variable names refer to the same object (same memory cell). http://ecomputernotes.com
Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards   sp
Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards   sp
Reference Variables Call stack layout after oldVal = oldVal - 3: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards   sp 28
Reference Variables Call stack layout after return from intMinus3: 1068 31 1072 28 caller’s other stuff   myInt retVal calling function “caller” stack grows downwards   sp 28
Reference Variables The compiler may actually implement call-by-reference using pointers as we did in example 2. The obtaining of address and de-referencing would be done behind the scene. We should think in terms of the “renaming” abstraction.  http://ecomputernotes.com

More Related Content

PDF
C++ Course - Lesson 2
PPTX
Chp4(ref dynamic)
PDF
Functions in python
PPT
PDF
Python Functions (PyAtl Beginners Night)
PDF
Python-02| Input, Output & Import
PPT
RECURSION IN C
PPTX
Pointers in C/C++ Programming
C++ Course - Lesson 2
Chp4(ref dynamic)
Functions in python
Python Functions (PyAtl Beginners Night)
Python-02| Input, Output & Import
RECURSION IN C
Pointers in C/C++ Programming

What's hot (19)

PPTX
Operating System Assignment Help
PDF
Storage classes arrays & functions in C Language
PDF
Python Modules, Packages and Libraries
PDF
Introduction to python
PPTX
PPTX
Pointer to function 2
PPT
Lecture 5
DOCX
Maharishi University of Management (MSc Computer Science test questions)
PDF
e computer notes - Reference variables
PPTX
Pointer basics
PPT
Prsentation on functions
PPTX
Pointer to function 1
PPTX
C++ lecture 03
PPT
Lecture 11 - Functions
PPTX
Function Pointer
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PPT
Pointers in C
PPTX
Learn c++ (functions) with nauman ur rehman
PDF
computer notes - Stack
Operating System Assignment Help
Storage classes arrays & functions in C Language
Python Modules, Packages and Libraries
Introduction to python
Pointer to function 2
Lecture 5
Maharishi University of Management (MSc Computer Science test questions)
e computer notes - Reference variables
Pointer basics
Prsentation on functions
Pointer to function 1
C++ lecture 03
Lecture 11 - Functions
Function Pointer
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Pointers in C
Learn c++ (functions) with nauman ur rehman
computer notes - Stack
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 37
PPT
computer notes - Data Structures - 36
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 19
computer notes - Data Structures - 26
computer notes - Data Structures - 31
computer notes - Data Structures - 7
computer notes - Data Structures - 5
computer notes - Data Structures - 25
computer notes - Data Structures - 2
computer notes - Data Structures - 18
computer notes - Data Structures - 1
computer notes - Data Structures - 37
computer notes - Data Structures - 36
computer notes - Data Structures - 32
computer notes - Data Structures - 8
computer notes - Data Structures - 11
computer notes - Data Structures - 21
computer notes - Data Structures - 6
computer notes - Data Structures - 27
computer notes - Data Structures - 12
computer notes - Data Structures - 39
computer notes - Data Structures - 33
computer notes - Data Structures - 19
Ad

Similar to computer notes - Data Structures - 17 (20)

PDF
computer notes - Reference variables
PPTX
chapter-7 slide.pptx
PPTX
Classes function overloading
PPTX
2-Concept of Pointers in c programming.pptx
PPT
Lecture06
PPT
C++ functions
PPTX
Pointers in c++ programming presentation
PPTX
Pointers lesson 5 (double pointer, call by value, call_by_reference)
PPTX
Chp3(pointers ref)
PPTX
stack.pptx
PDF
4th unit full
PDF
intro_to_cpp_namespace_robotics_corner.pdf
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PDF
computer notes - Reference variables ii
PPTX
Lecture 4: Functions
PPTX
1. DSA - Introduction.pptx
PPTX
PPT
Lecture#7 Call by value and reference in c++
PPT
16717 functions in C++
 
PPTX
How Functions Work
computer notes - Reference variables
chapter-7 slide.pptx
Classes function overloading
2-Concept of Pointers in c programming.pptx
Lecture06
C++ functions
Pointers in c++ programming presentation
Pointers lesson 5 (double pointer, call by value, call_by_reference)
Chp3(pointers ref)
stack.pptx
4th unit full
intro_to_cpp_namespace_robotics_corner.pdf
FUNCTIONS, CLASSES AND OBJECTS.pptx
computer notes - Reference variables ii
Lecture 4: Functions
1. DSA - Introduction.pptx
Lecture#7 Call by value and reference in c++
16717 functions in C++
 
How Functions Work

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 10
computer notes - Data Structures - 30
computer notes - Data Structures - 20
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 4
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
computer notes - Data Structures - 10

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Approach and Philosophy of On baking technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
MYSQL Presentation for SQL database connectivity
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Machine Learning_overview_presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Tartificialntelligence_presentation.pptx
Assigned Numbers - 2025 - Bluetooth® Document
The Rise and Fall of 3GPP – Time for a Sabbatical?
Network Security Unit 5.pdf for BCA BBA.
Approach and Philosophy of On baking technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Empathic Computing: Creating Shared Understanding
Machine Learning_overview_presentation.pptx
Unlocking AI with Model Context Protocol (MCP)

computer notes - Data Structures - 17

  • 1. Class No.17 Data Structures http://ecomputernotes.com
  • 2. Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is the address operator, i.e., it returns the address of the variable in memory. int x; int* ptr = &x; http://ecomputernotes.com
  • 3. Reference Variables The symbol “&’ can also appear after a type in a function signature: For example, insert and remove from the BinarySearchTree class. void insert( const EType& x ); void remove( const EType& x ); http://ecomputernotes.com
  • 4. Reference Variables Or, in case we designed the BinarySearchTree class to hold integers only, i.e., no templates void insert( const int& x ); void remove( const int& x ); http://ecomputernotes.com
  • 5. Reference Variables The “&” indicates a parameter that is a reference variable . Consider the following three different functions: http://ecomputernotes.com
  • 6. Reference Variables // example 1 int intMinus1( int oldVal) { oldVal = oldVal – 1; return oldVal; } http://ecomputernotes.com
  • 7. Reference Variables // example 2 int intMinus2( int* oldVal) { *oldVal = *oldVal – 2; return *oldVal; } http://ecomputernotes.com
  • 8. Reference Variables // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
  • 9. Reference Variables The caller function: calling intMinus1 void caller() { int myInt = 31; int retVal; retVal = intMinus1( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
  • 10. Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (ourtest.exe) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
  • 11. Reference Variables Call stack layout Parameters(caller) Local variables(caller) Return address(caller) Parameters(intMinus1) Local variables(intMinus1) Return address(intMinus1) sp Stack grows downwards http://ecomputernotes.com
  • 12. Reference Variables Call stack layout when intMinus1 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards sp
  • 13. Reference Variables How are myInt and oldVal related? Passing myInt to the function intMinus1 results in a copy of myInt to be placed in parameter oldVal in the call stack. Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
  • 14. Reference Variables The original myInt remains unchanged . For this reason, this technique of passing parameters is called pass by value . Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
  • 15. Reference Variables Call stack layout after subtraction in intMinus1: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards sp 30
  • 16. Reference Variables Call stack layout after return from intMinus1: 1068 31 1072 30 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp
  • 17. Reference Variables We could have called intMinus1 as void caller() { int retVal; retVal = intMinus1( 31 );// literal cout << myInt << retVal; } Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value. http://ecomputernotes.com
  • 18. Reference Variables If the programmer wanted to actually change a variable’s value from within a function, one way would be to send a pointer: void caller() { int retVal; int myInt = 31; retVal = intMinus2( &myInt ); cout << myInt << retVal; } Call this call-by-pointer. http://ecomputernotes.com
  • 19. Reference Variables Call stack layout when intMinus2 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards sp
  • 20. Reference Variables Call stack layout after *oldVal = *oldVal – 2; 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards sp 29
  • 21. Reference Variables Call stack layout after return from intMinus2. 1068 31 1072 29 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp 29
  • 22. Reference Variables Suppose we want a function to change an object. But we don’t want to send the function a copy. The object could be large and copying it costs time. We don’t want to use pointers because of the messy syntax. http://ecomputernotes.com
  • 23. Reference Variables The answer: call-by-reference (or pass-by-reference): void caller() { int retVal; int myInt = 31; retVal = intMinus3( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
  • 24. Reference Variables The & after int means that oldVal is an integer reference variable. // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
  • 25. Reference Variables So what is a reference variable? The idea is: the integer object myInt is used exactly as it exists in the caller. The function simply reaches it through a different name , oldVal. The function intMinus3 cannot use the name myInt because it is in the caller’s scope. But both variable names refer to the same object (same memory cell). http://ecomputernotes.com
  • 26. Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp
  • 27. Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp
  • 28. Reference Variables Call stack layout after oldVal = oldVal - 3: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp 28
  • 29. Reference Variables Call stack layout after return from intMinus3: 1068 31 1072 28 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp 28
  • 30. Reference Variables The compiler may actually implement call-by-reference using pointers as we did in example 2. The obtaining of address and de-referencing would be done behind the scene. We should think in terms of the “renaming” abstraction. http://ecomputernotes.com

Editor's Notes

  • #3: Start of lecture 17
  • #13: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #16: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #17: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #20: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #21: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #22: End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #28: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #29: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #30: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #31: End of Lecture 17